﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using ClientDependency.Core.Controls;
using ClientDependency.Core;

namespace DigibizTree.Shared
{
	/// <summary>
	/// Extension methods for embedded resources
	/// </summary>
	public static class DigibizResourceExtensions
	{
		/// <summary>
		/// Adds an embedded resource to the ClientDependency output by name.
		/// </summary>
		/// <param name="ctl">The CTL.</param>
		/// <param name="resourceName">Name of the resource.</param>
		/// <param name="type">The type.</param>
		public static void AddResourceToClientDependency(this Control ctl, string resourceName, ClientDependencyType type)
		{
			//Get the urls for the embedded resources           .
			var resourceUrl = ctl.Page.ClientScript.GetWebResourceUrl(ctl.GetType(), resourceName);

			//This only works in v4 currently or until CD version 1.2, so in the meantime, use the below method to add the resources to client dependency.
			switch (type)
			{
				case ClientDependencyType.Css:
					ctl.Page.Header.Controls.Add(
						new LiteralControl("<link type='text/css' rel='stylesheet' href='" + resourceUrl + "' />"));
					break;

				case ClientDependencyType.Javascript:
                    ctl.Page.ClientScript.RegisterClientScriptResource(typeof(DigibizResourceExtensions), resourceName);
					break;

				default:
					break;
			}

		}


	}
}
